package com.zking;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo4 {
    public static void main(String[] args) {
        String str="my name is 007";
        //返回的结果是乱的
        str.chars().parallel().forEach(i-> System.out.println((char)i));
        //使用ForEachOredered保证顺序
        str.chars().parallel().forEachOrdered(i-> System.out.println((char) i));
        //收集到list
        List<String> collect = Stream.of(str.split("")).collect(Collectors.toList());
        System.out.println(collect);

        //Optional 避免加一些空判断  使用reduce拼接字符串
        Optional<String> reduce = Stream.of(str.split("")).reduce((s1, s2) -> s1 + "" + s2);
        System.out.println(reduce.orElse(""));
    }
}
